	
	package servlet.user;
	
	import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import enitys.User;
		
import service.UserManager;
	
	public class FindInfoByName extends HttpServlet {
	
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	
		/**
		 * Constructor of the object.
		 */
		public FindInfoByName() {
			super();
		}
	
		/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
				doPost(request,response);
		}
	
		/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
					HttpSession session=request.getSession();
					int shenfen=Integer.parseInt(session.getAttribute("shenfen").toString());
					if(shenfen==1){
						String name=request.getParameter("username");
						UserManager manager = UserManager.getInstance();
						List<User> info = new ArrayList<User>();
						try {
							info.add(manager.getUserByName(name));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						String message = null;
						//根据返回的结果向request中设置提示信息
						if(info.get(0).getS_id()==0){
							message = "没有此用户！";
							info.remove(0);
						}else{
							message = "结果如下：";
						}
						request.setAttribute("message", message);
						//把结果集放进request中
						request.setAttribute("user",info);
						//跳转页面
				request.getRequestDispatcher("/jsp/user_info/s_mananger.jsp").forward(request, response);
			}
			if(shenfen==0){
				String name=request.getSession().getAttribute("username").toString();
				UserManager manager = UserManager.getInstance();
				List<User> info = new ArrayList<User>();
				try {
					info.add(manager.getUserByName(name));
					request.setAttribute("user",info);
					request.getRequestDispatcher("/jsp/user_info/s_info.jsp").forward(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
